﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using JCTimes.DomainModel.Repository;
using JCTimes.DomainModel.Views;
using JCTimes.Portal.Infrastructure;
using JCTimes.Portal.Models;
using JCTimes.Portal.Specification;
using System.Data;
namespace JCTimes.Portal.Controllers
{
     [Permission]
    public class PDALogController : Controller
    {
        //
        // GET: /PDALog/
        private IPDARepository dmRep = null;
        private const int pageSize = 10;

        public PDALogController(IPDARepository dmRep)
        {
            this.dmRep = dmRep;
        }

        public ActionResult Index()
        {
            return View();
        }

        public PartialViewResult Grid(int? pageIndex, Searcher searcher)
        {

            var pred = PDALogSpecification.Get_V_Lst_PDALog_Pred(searcher);
            var model = new GridView<V_PDALog>
            {
                Searcher = searcher,
                PagedList = dmRep.GetList<V_PDALog>().Where(pred).OrderByDescending(u => u.CreateDate).ToPagedList(pageIndex ?? 1, pageSize)
            };
            return PartialView("_gridview", model);
        }

        [ActionName("Export")]
        [HttpPost]
        public JsonResult PreExportSellThrough(Searcher searcher)
        {
            var pred = PDALogSpecification.Get_V_Lst_PDALog_Pred(searcher);

            var list = dmRep.GetList<V_PDALog>();

            bool hasdata = list.Where(pred).Count() > 0;
            return Json(new { searcher = searcher.ToString(), hasdata = hasdata });
        }


        /// <summary>
        /// export for users log
        /// </summary>
        /// <param name="searcher"></param>
        public void Export(Searcher searcher)
        {
            var pred = PDALogSpecification.Get_V_Lst_PDALog_Pred(searcher);
            var list = dmRep.GetList<V_PDALog>();
            var lst = list.Where(pred).OrderByDescending(d => d.CreateDate).ToList();

            string[] filters = new string[] {
                "PDANo","PortName","LogContent","ConnectFlag", "CreateDate"
               
            };

            var dt = lst.ToDataTable(filters);
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    string pdano = dr["PDANo"] + "";
                    string logcontent = dr["LogContent"] + "";
                    if (string.IsNullOrEmpty(pdano))
                    {
                        pdano = logcontent;
                        dr["PDANo"] = pdano;
                    }
                }
            }
            string[] columnname = new string[] { "PDA编号", "接口名称", "日志内容", "是否合法", "创建时间" };

            ExcelHelper.ExportToExcel(null, "PDALog", dt, columnname, filters, false);
        }


    }
}
